package com.neutech.Ydelete;
//        resp.setContentType("application/json");
//        else {
//        }
//        ResultVO businessList = businessService.listBusiness();
//        resp.setContentType("application/json;charset=utf-8");
//        ResultVO resultVO = ResultVO.success(businessList);


import com.google.gson.Gson;
import com.neutech.entity.Business;
import com.zaxxer.hikari.HikariDataSource;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//@WebServlet("/businessList")
//@Override

//protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
////        System.out.println("正常逻辑");
//    this.doPost(req,resp);
//}
//
//@Override
//protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//
//}
//String username = req.getParameter("username")

public class FeiQi extends HttpServlet {

    public static final  HikariDataSource dataSource;

    static {

        dataSource = new HikariDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/ele");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Business> businessList = new ArrayList<>();

        try {
            connection = dataSource.getConnection();
            preparedStatement = connection.prepareStatement("select * from e_business");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {

                Business business = new Business();
                business.setId(resultSet.getInt("id"));
                business.setName(resultSet.getString("name"));
                business.setAddress(resultSet.getString("address"));
                business.setDescription(resultSet.getString("description"));
                business.setMainImage(resultSet.getString("main_image"));
                business.setStartPrice(resultSet.getBigDecimal("start_price"));
                business.setDeliverPrice(resultSet.getBigDecimal("delivery_price"));

                businessList.add(business);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            if (connection != null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }

            }
            if (preparedStatement != null){
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }

            }
            if (resultSet != null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        out.println(new Gson().toJson(businessList));

        out.close();

    }
}
